package com.cloud.io;

import java.io.*;

/**
 * BufferedInputStream是带缓冲区的输入流，
 * 它继承于FilterInputStream。
 * 默认缓冲区大小是8M，能够减少访问磁盘的次数，提高文件读取性能
 *
 * BufferedOutputStream是带缓冲区的输出流，
 * 它继承于FilterOutputStream，能够提高文件的写入效率
 */
public class BufferedStreamTest {

    public static void readAndWrite(String src, String des) {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(src));
            bos = new BufferedOutputStream(new FileOutputStream(des));
            byte[] b = new byte[1024];
            int len = 0;
            while((len = bis.read(b, 0, b.length)) != -1) {
                bos.write(b, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(bos != null) {
                try {
                    bos.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }

            }
            if(bis != null) {
                try {
                    bis.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }

            }
        }
    }

    public static void main(String[] args) {
        readAndWrite("src/main/resources/shu.txt", "src/main/resources/shu2.txt");
    }
}
